﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-16
Description:Sms_Log三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class Sms_LogService
	{
		#region 增加一条数据
		public static int AddSms_Log(Sms_Log sms_Log)
		{
			string sql =
			"INSERT INTO Sms_Log(sendtime,senderID,target,scontent,state,statetime,smsguid,taskstate,shenhe,sendcount,lockguid) "+
			"VALUES (@sendtime,@senderID,@target,@scontent,@state,@statetime,@smsguid,@taskstate,@shenhe,@sendcount,@lockguid)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@sendtime",sms_Log.Sendtime),
						
					new SqlParameter("@senderID",sms_Log.SenderID),
						
					new SqlParameter("@target",sms_Log.Target),
						
					new SqlParameter("@scontent",sms_Log.Scontent),
						
					new SqlParameter("@state",sms_Log.State),
						
					new SqlParameter("@statetime",sms_Log.Statetime),
						
					new SqlParameter("@smsguid",sms_Log.Smsguid),
						
					new SqlParameter("@taskstate",sms_Log.Taskstate),
						
					new SqlParameter("@shenhe",sms_Log.Shenhe),
						
					new SqlParameter("@sendcount",sms_Log.Sendcount),
						
					new SqlParameter("@lockguid",sms_Log.Lockguid)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSms_LogById(long id)	
		{
			string sql="DELETE "+"Sms_Log"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySms_LogByid(Sms_Log sms_Log)
		{
		string sql ="UPDATE "+"Sms_Log"+" set "+
					"sendtime=@sendtime, "+
					"senderID=@senderID, "+
					"target=@target, "+
					"scontent=@scontent, "+
					"state=@state, "+
					"statetime=@statetime, "+
					"smsguid=@smsguid, "+
					"taskstate=@taskstate, "+
					"shenhe=@shenhe, "+
					"sendcount=@sendcount, "+
					"lockguid=@lockguid "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",sms_Log.Id),
					new SqlParameter("@sendtime",sms_Log.Sendtime),
					new SqlParameter("@senderID",sms_Log.SenderID),
					new SqlParameter("@target",sms_Log.Target),
					new SqlParameter("@scontent",sms_Log.Scontent),
					new SqlParameter("@state",sms_Log.State),
					new SqlParameter("@statetime",sms_Log.Statetime),
					new SqlParameter("@smsguid",sms_Log.Smsguid),
					new SqlParameter("@taskstate",sms_Log.Taskstate),
					new SqlParameter("@shenhe",sms_Log.Shenhe),
					new SqlParameter("@sendcount",sms_Log.Sendcount),
					new SqlParameter("@lockguid",sms_Log.Lockguid)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Sms_Log> GetAllSms_Logs()
		{
			string sqlAll = "SELECT * FROM Sms_Log";
			return GetSms_LogsBySql(sqlAll);
		}
		//以标识列获得对象
		public static Sms_Log GetSms_LogByid(object id)
		{
			string sql = "SELECT * FROM "+"Sms_Log"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Sms_Log sms_Log = new Sms_Log();
					sms_Log.Id=Convert.ToInt32(reader["id"]);
					sms_Log.Sendtime=Convert.ToDateTime(reader["sendtime"]);
					sms_Log.SenderID=Convert.ToString(reader["senderID"]);
					sms_Log.Target=Convert.ToString(reader["target"]);
					sms_Log.Scontent=Convert.ToString(reader["scontent"]);
					sms_Log.State=Convert.ToString(reader["state"]);
					sms_Log.Statetime=Convert.ToDateTime(reader["statetime"]);
					sms_Log.Smsguid=Convert.ToString(reader["smsguid"]);
					sms_Log.Taskstate=Convert.ToString(reader["taskstate"]);
					sms_Log.Shenhe=Convert.ToString(reader["shenhe"]);
					sms_Log.Sendcount=Convert.ToInt32(reader["sendcount"]);
					sms_Log.Lockguid=Convert.ToString(reader["lockguid"]);
					reader.Close();
					
					return sms_Log;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Sms_Log> GetSms_LogsBySql(string safeSql)
		{
			List<Sms_Log> list = new List<Sms_Log>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_Log sms_Log = new Sms_Log();
					sms_Log.Id=Convert.ToInt32(row["id"]);
					sms_Log.Sendtime=Convert.ToDateTime(row["sendtime"]);
					sms_Log.SenderID=Convert.ToString(row["senderID"]);
					sms_Log.Target=Convert.ToString(row["target"]);
					sms_Log.Scontent=Convert.ToString(row["scontent"]);
					sms_Log.State=Convert.ToString(row["state"]);
					sms_Log.Statetime=Convert.ToDateTime(row["statetime"]);
					sms_Log.Smsguid=Convert.ToString(row["smsguid"]);
					sms_Log.Taskstate=Convert.ToString(row["taskstate"]);
					sms_Log.Shenhe=Convert.ToString(row["shenhe"]);
					sms_Log.Sendcount=Convert.ToInt32(row["sendcount"]);
					sms_Log.Lockguid=Convert.ToString(row["lockguid"]);
					list.Add(sms_Log);
				}
				return list;
			}
             
		}
		private static IList<Sms_Log> GetSms_LogsBySql( string sql, params SqlParameter[] values )
		{
			List<Sms_Log> list = new List<Sms_Log>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_Log sms_Log = new Sms_Log();
					sms_Log.Id=Convert.ToInt32(row["id"]);
					sms_Log.Sendtime=Convert.ToDateTime(row["sendtime"]);
					sms_Log.SenderID=Convert.ToString(row["senderID"]);
					sms_Log.Target=Convert.ToString(row["target"]);
					sms_Log.Scontent=Convert.ToString(row["scontent"]);
					sms_Log.State=Convert.ToString(row["state"]);
					sms_Log.Statetime=Convert.ToDateTime(row["statetime"]);
					sms_Log.Smsguid=Convert.ToString(row["smsguid"]);
					sms_Log.Taskstate=Convert.ToString(row["taskstate"]);
					sms_Log.Shenhe=Convert.ToString(row["shenhe"]);
					sms_Log.Sendcount=Convert.ToInt32(row["sendcount"]);
					sms_Log.Lockguid=Convert.ToString(row["lockguid"]);
					list.Add(sms_Log);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<Sms_Log> ShowSms_LogForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from Sms_Log where  id not in ( select top "
						   +number+" id from Sms_Log order by id desc ) order by id desc";
			return GetSms_LogsBySql(strsql);
		}
		#endregion
	}
}

